package com.cy.aircompressor.main.view.pop;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cy.aircompressor.R;
import com.cy.aircompressor.entity.compressor.Compressor;
import com.cy.aircompressor.entity.select.SelectDict;
import com.drug.lib.view.PopupWindow.BasePopupWindow;

import java.util.List;

/**
 * Description: MyPopupWindow
 * Copyright  : Copyright (c) 2021
 * Author     : mliuxb
 * Date       : 2021-05-01 21:17:06
 */
public class AirDropdownPopupWindowV2 extends BasePopupWindow {
    private List<Compressor> compressors;
    private final TextView mTextView;//用于PopupWindow定位的TextView
    private View contentView;
    private RecyclerView recyclerView;

    public AirDropdownPopupWindowV2(@NonNull Context context, @NonNull TextView textView, View contentView, List<Compressor> compressors) {
        super(context);
        this.mTextView = textView;
        this.contentView = contentView;
        setWidth(contentView.getWidth());
        setBackgroundDrawable(new ColorDrawable());
        this.compressors = compressors;
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new PopupAdapter());
        recyclerView.setBackgroundColor(Color.parseColor("#236C8A"));
    }


    @Override
    protected boolean isOutsideTouch() {
        return true;
    }

    @Override
    protected View getContentLayout() {
        recyclerView = new RecyclerView(mContext);
        return recyclerView;
    }

    @Override
    protected void initView() {

    }

    private class PopupAdapter extends RecyclerView.Adapter<PopupHolder> {

        @NonNull
        @Override
        public PopupHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            final Context context = parent.getContext();//context是当前Activity的对象
            final int dip10 = dip2px(context, 10);
            //final int dip15 = dip2px(context, 15);
            final TextView textView = new TextView(context);
            textView.setWidth(contentView.getWidth());

            textView.setGravity(Gravity.START);
            textView.setTextSize(9);
            textView.setTextColor(context.getResources().getColor(R.color.white));
//            textView.setTextColor(context.getResources().getColorStateList(R.color.selector_text_blue_orange));
            textView.setPadding(dip10, dip10, dip10, dip10);
            //textView.setBackgroundResource(R.drawable.selector_recycler_item_gray_sky);
            return new PopupHolder(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull final PopupHolder holder, int position) {
            Compressor compressor = compressors.get(position);
            holder.textView.setText(compressor.getName());

            holder.textView.setOnClickListener(v -> {
                mTextView.setText(holder.textView.getText());
                if (isShowing()) {
                    if (dropdownCallBack != null)
                        dropdownCallBack.select(compressor);
                    dismiss();
                }
            });
        }

        @Override
        public int getItemCount() {
            return compressors.size();
        }
    }


    private static class PopupHolder extends RecyclerView.ViewHolder {

        private final TextView textView;

        private PopupHolder(@NonNull TextView textView) {
            super(textView);
            this.textView = textView;
        }
    }

    // 将dip或dp值转换为px值
    private int dip2px(Context context, double dipValue) {
        final double scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    DropdownCallBack dropdownCallBack;

    public void setDropdownCallBack(DropdownCallBack dropdownCallBack) {
        this.dropdownCallBack = dropdownCallBack;
    }

    public interface DropdownCallBack {
        void select(Compressor selectDict);
    }
}
